package com.iwop.moto.controller;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SMS;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.SMSService;
import com.iwop.moto.utils.http.SMSUtil;
import com.iwop.moto.utils.stringUtil.JsonUtils;
import com.iwop.moto.utils.stringUtil.RandomCodeUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 短信验证码
 * @author  Created by huyuangui on 2016/7/11.
 */
@Controller
public class SMSController extends BaseController {
    @Autowired
    private SMSService smsService;
    /***
     *根据手机号码获取短信验证码
     * @param request
     * @return
     */
    @RequestMapping("/sendSMS")
      @ResponseBody
      public ResultModel sendSMS(HttpServletRequest request) throws IOException {
        ResultModel resultModel=new ResultModel();
        String tel= request.getParameter("tel");
        if(StringUtils.isEmpty(tel)){
            resultModel.setCode(0);
            resultModel.setMem(ErrCode.ERROR_TEL_ISNOTNULL);
            return resultModel;
        }
        //验证手机号码是否正确
        if(false==StringUtils.isMobile(tel)) {
            resultModel.setCode(0);
            resultModel.setMem(ErrCode.ERROR_TEL_FORMAT);
            return resultModel;
        }


        SMS sms=new SMS();
        sms.setTel(tel.trim());
        sms=smsService.findByTelOrId(sms);
        if(sms != null && sms.getStatus().equals("0") ){//获取该手机号最近一条验证码记录
            Long nowTime = new Date().getTime();//当前时间毫秒
            Long smsTime = sms.getAddtime().getTime();
            long last = smsTime + (59 * 1000);
            if(nowTime < last){//限制1分钟之内一个手机号只能发送一次验证码
                return getErr(ErrCode.ERROR_SMS_CODE_LIMIT);
            }
        }else{
            sms=new SMS();
            sms.setTel(tel);
        }
        //判断时间
//        Date pre3Date=new Date();
//        pre3Date= DateUtils.addMinute(pre3Date,-10);
//        if(sms!=null && sms.getStatus().equals("0") && sms.getAddtime().compareTo(pre3Date)>0){
//            //3分钟前已经发送过
//            return resultModel;
//        }else{
//            sms=new SMS();
//            sms.setTel(tel);
//        }
        sms.setStatus("0");
        sms.setAddtime(new Date());
        sms.setContent(RandomCodeUtil.random(4));
        smsService.save(sms);
        //发送一个4位数的短信
//        String result = SMSUtil.sendContent(tel, sms.getContent());//旧短信接口
//        Map map = JsonUtils.toMap(result);
//        if(map.get("Code").toString().equals("-1")){
//            resultModel.setCode(0);
//            resultModel.setMem(map.get("Message").toString());
//        }
        String result = SMSUtil.sendYunXinSms(tel, sms.getContent());//云信短信接口
        Map map = JsonUtils.toMap(result);
        if(!map.get("code").toString().equals("200")){
            resultModel.setCode(0);
            resultModel.setMem(map.get("msg").toString());
        }
        return resultModel;
    }


    /**
     * @功能: 验证短信验证码是否有效
     * @作者: liuwb by 2016-09-09 on create
     * @参数: tel 手机号
     * @参数: code 验证码
     */
    @RequestMapping("/sms/check/code")
    @ResponseBody
    public ResultModel checkSmsCodeByTel(){
        SuperRecordModel srm  = this.getSRModel();
        SMS sms=new SMS();
        try {
            sms.setTel(srm.getString("tel"));
            sms.setContent(srm.getString("code"));
            sms= smsService.findCheckCodeByTel(sms);
            Long nowTime = new Date().getTime();//当前时间毫秒
            if(sms == null){
                return getErr(ErrCode.ERROR_SMS);
            } else if(sms.getStatus() == null || "1".equals(sms.getStatus())){
                return getErr(ErrCode.ERROR_SMS_USED);
            }
            Long smsTime = sms.getAddtime().getTime();
            if(nowTime > (smsTime + (300 * 1000)) ){//判断是否5分钟之内
                return getErr(ErrCode.ERROR_SMS_OVERDUE);
            }
        }catch (Exception e){
            e.printStackTrace();
            getErr(ErrCode.ERROR_SYSTEM);
        }
        return getSucceed(ErrCode.SUCCEED);
    }


}
